#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <cassert>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

using std::string;

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    typedef struct Question
    {
        /* data */
        string number;     // 题目编号
        string title;      // 题目标题
        string difficulty; // 题目难度
        int cpu_limit;     // 时间要求(sec)
        int mem_limit;     // 空间要求(KB)
        string desc;       // 题目描述
        string header;     // 题目预设代码
        string tail;       // 测试用例
    }Question;

    const string question_list = "./questions/questions.list";
    const string question_path = "./questions/";

    class Model
    {
    private:
        std::unordered_map<string, Question> questions;

    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }
        ~Model() {}
        bool LoadQuestionList(const string &question_list)
        {
            // Load: questions/question_list + number
            std::ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "加载题库失败,题库不存在\n";
                return false;
            }
            string line = "";
            while (getline(in, line))
            {
                std::vector<string> tokens;
                StringUtil::SplitString(&tokens, line, " ");
                if (tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败,请检查文件格式\n";
                    continue;
                }
                Question q;
                // 1 判断回文数 简单 1 30000
                q.number = tokens[0];
                q.title = tokens[1];
                q.difficulty = tokens[2];
                q.cpu_limit = std::stoi(tokens[3]);
                q.mem_limit = std::stoi(tokens[4]);

                string q_num_path = question_path + q.number + "/";

                FileUtil::ReadFile(q_num_path + "desc.txt", &q.desc, true);
                FileUtil::ReadFile(q_num_path + "header.cpp", &q.header, true);
                FileUtil::ReadFile(q_num_path + "tail.cpp", &q.tail, true);

                questions.insert({q.number, q});
            }
            LOG(INFO) << "加载题库完成\n";
            return true;
        }
        bool GetAllQuestions(std::vector<Question> *out)
        {
            if (questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败\n";
                return false;
            }
            for (const auto &q : questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetNumQuestion(const string &number, Question *q)
        {
            const auto &iter = questions.find(number);
            if (iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败,编号:" << number << "\n";
                return false;
            }
            (*q) = iter->second;
            return true;
        }
    };
}